package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    
    @Autowired
    private MongoTemplate template;

    @Autowired
    private StrategyRepository strategyRepository;
    
    
    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).get();
    }

    @Override
    public List<StrategyCatalog> selectAll() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        //创建Query对象,根据过滤条件查询总条数,此处没有限制条件,所以用空的查询对象查
        Query query = new Query();
        //创建过滤查询
        //有数据,进行分页操作,创建新的分页对象,此索引从0开始,所以要减一,分页的字段与数据库一致
        Pageable pageAble = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        //返回给控制器
        return DBHelper.query(template, StrategyCatalog.class, query, pageAble);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void deleteById(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public List<CatalogVO> groupList() {
        List<CatalogVO> list = new ArrayList<>();       //最终返回的东西
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();
        //datas即为查出来的所有数据
        //为Map的集合,每一行对应一个Map对象
        //分析一个Map对象,里面的元素为  _id=广州  names=[长隆度假区,最广州,广式美味]   ids=[上面三个攻略分类的id]
        //遍历Map集合的集合
        for (Map data : datas) {
            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setDestName(data.get("_id").toString());
            //再遍历
            List<Object> names = (List<Object>) data.get("names");
            List<Object> ids = (List<Object>) data.get("ids");

            List<StrategyCatalog> catalogs=new ArrayList<>();

            for (int i = 0; i < names.size(); i++) {
                StrategyCatalog catalog = new StrategyCatalog();

                catalog.setId(ids.get(i).toString());

                catalog.setName(names.get(i).toString());

                catalogs.add(catalog);

            }
            catalogVO.setMapList(catalogs);
            list.add(catalogVO);
        }
        return list;
    }

    @Override
    public void addCatalog(Strategy strategy) {
        /**
         * 给攻略对象添加分类名
         * @param strategy
         */
        StrategyCatalog sc = strategyCatalogRepository.findById(strategy.getCatalogId()).get();
        strategy.setCatalogName(sc.getName());
    }

    @Override
    public List<StrategyCatalog> selectCatalogsAndStraById(String destId) {
        /**
         * 根据目的地id查询目的地下所有攻略分类及分类下的攻略
         * @param destId
         * @return
         */
        List<StrategyCatalog> catalogs = strategyCatalogRepository.findByDestId(destId);
        //遍历攻略分类,向strategies属性中添加具体攻略
        for (StrategyCatalog catalog : catalogs) {
            //拿取某个攻略分类下的所有攻略
            List<Strategy> strategies = strategyRepository.findByCatalogId(catalog.getId());
            catalog.setStrategies(strategies);
        }
        return catalogs;

    }
}
